﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Specialized;
using Microsoft.SharePoint.Utilities;

namespace SPN.Layouts.SPN.pages
{
    public partial class NewsletterAdd : LayoutsPageBase
    {
        #region PageEvents
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SPWeb _Web = SPContext.Current.Web;
                hl_Cancel.NavigateUrl = _Web.Url + "/_layouts/SPN/pages/Newsletter.aspx";
            }
        }
        #endregion

        #region ControlsEvents
        protected void lib_Save_Click(object sender, EventArgs e)
        {
            SPWeb _Web = SPContext.Current.Web;
            string _SubscriberEmailFrom = Common.Helper.GetWebProperty(_Web, Common.Helper._SPN_SubscribeEmailFrom);
            if (Common.Helper.IsListValid(_Web, Common.Helper._Const_NewsletterListName) && Common.Helper.IsListValid(_Web, Common.Helper._Const_SubscriberListName))
            {
                SPList _NewsletterList = _Web.Lists[Common.Helper._Const_NewsletterListName];
                SPListItemCollection _NewsletterListItems = _NewsletterList.Items;
                SPListItem _NewsletterListItem = _NewsletterListItems.Add();
                _NewsletterListItem["Title"] = tb_Title.Text;
                _NewsletterListItem["Content"] = tb_Content.Text;
                _NewsletterListItem.Update();
                //===================================================================>>
                int _NumberOfSentEmails = 0;
                SPList _SubscriberList = _Web.Lists[Common.Helper._Const_SubscriberListName];
                SPListItemCollection _SubscriberListItems = _SubscriberList.Items;
                if (_SubscriberListItems.Count > 0)
                {
                    int _NumberCicles = (_SubscriberListItems.Count / 50) + 1;
                    for (int i = 0; i < _NumberCicles; i++)
                    {
                        string _BulkList = "";
                        int _iValue = i * 50;
                        int _iMaxValue = (i * 50) + 50;
                        if (_iMaxValue > _SubscriberListItems.Count)
                        {
                            _iMaxValue = _SubscriberListItems.Count;
                        }
                        for (int j = _iValue; j < _iMaxValue; j++)
                        {
                            SPListItem _Subscriber = _SubscriberListItems[j];
                            _BulkList += _Subscriber["Title"].ToString() + ";";
                            _NumberOfSentEmails++;
                        }
                        StringDictionary _EmailHeaders = new StringDictionary();
                        _EmailHeaders.Add("bcc", _BulkList);
                        _EmailHeaders.Add("from", _SubscriberEmailFrom);
                        _EmailHeaders.Add("subject", tb_Title.Text);
                        _EmailHeaders.Add("content-type", "text/html");
                        SPUtility.SendEmail(_Web, _EmailHeaders, tb_Content.Text);
                    }
                }
            }
            Response.Redirect(_Web.Url + "/_layouts/SPN/pages/Newsletter.aspx");
        }
        #endregion

        #region Methods

        #endregion
    }
}
